package user_v1

import (
	"context"
	"errors"
	"gf-xb-api/internal/consts"
	"gf-xb-api/internal/dao"
	"gf-xb-api/internal/model/entity"
)

func (s *sUser) Enable(ctx context.Context, username string) (output bool, err error) {
	var user *entity.SysUser

	cols := dao.SysUser.Columns()

	err = dao.SysUser.Ctx(ctx).Where(cols.Username, username).Scan(&user)
	if err != nil {
		return false, err
	}

	if user == nil {
		return false, errors.New(consts.USER_NOT_FOUND)
	}

	active := uint(consts.USER_STATUS_ACTIVE)
	if user.Status == active {
		return true, nil
	}

	_, err = dao.SysUser.Ctx(ctx).Data(cols.Status, active).WherePri(user.Id).Update()
	if err != nil {
		return false, nil
	}

	return true, nil
}
